package mi.payment.transaction;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import mi.payment.R;
import mi.payment.admin.pay88.IPay88PaySelectActivity;
import mi.payment.transaction.sale.ConsumeSwingCardActivity;
import mi.payment.ui.base.AmountInputView;
import mi.payment.utils.MToast;

public class PayModeView extends FrameLayout implements View.OnClickListener {

    private View mView;
    private Context mContext;
    private LinearLayout mContainerView;
    private ParamsCheckExt mParamsCheckExt;
    private AmountInputView mAmountInputView;

    public enum Item {
        BANK,               // 银行卡
        USE_PWD_BANK,       // 凭密消费

        ALL_PAY_SCAN,       // 商户扫客户
        ALL_PAY_CODE,       // 客户扫商户

        WE_CHAT_SCAN,       // 微信，商户扫客户
        WE_CHAT_QR,         // 微信，客户扫商户

        ALI_PAY_SCAN,       // 支付宝，商户扫客户
        ALI_PAY_QR,         // 支付宝，客户扫商户

        KOU_BEI_SCAN,       // 口碑，商户扫客户
        KOU_BEI_QR,         // 口碑，客户扫商户

        UNION_PAY_SCAN,     // 银联扫码，商户扫客户
        UNION_PAY_QR        // 银联扫码，客户扫商户
    }

    public PayModeView(Context context) {
        super(context);
        initView();
    }

    public PayModeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PayModeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setAmountInputView(AmountInputView amount) {
        this.mAmountInputView = amount;
    }

    public void setParamsCheckExt(ParamsCheckExt ext) {
        this.mParamsCheckExt = ext;
    }

    public void initView() {
        if (mView == null) {
            mContext = getContext();
            mView = View.inflate(mContext, R.layout.view_pay_mode, this);
            mContainerView = mView.findViewById(R.id.view_container);
        }
        mContainerView.removeAllViews();

        // 根据支付方式的数量加载不同的UI布局
        List<HomePayModeItem> list = getPayModeItemList();
        int size = list.size();
        if (size == 1) {
            Button bankCard = getBankCardPay();
            mContainerView.setPadding(68, 0, 0, 0);
            mContainerView.addView(bankCard);
        } else {
            mContainerView.setPadding(34, 0, 0, 34);
            setAppropriateLayoutParams(list);
        }
    }

    private void setAppropriateLayoutParams(List<HomePayModeItem> list) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            HomePayModeItem payModeItem = list.get(i);
            LinearLayout itemView = payModeItem.findViewById(R.id.view_item);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) itemView.getLayoutParams();
            if (size == 2) {
                layoutParams.width = 310;
                layoutParams.height = 160;
                if (i > 0) {
                    layoutParams.leftMargin = 30;
                }
            } else if (size == 3) {
                if (i > 0) {
                    layoutParams.leftMargin = 25;
                }
            } // else {} do nothing
            layoutParams.topMargin = 23;
            itemView.setLayoutParams(layoutParams);
            payModeItem.setOnClickListener(this);

            mContainerView.addView(payModeItem);
        }
    }

    private Button getBankCardPay() {
        Button button = new Button(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.width = 592;
        layoutParams.height = 90;
        button.setLayoutParams(layoutParams);
        button.setText(R.string.platform_bank_card_consumption);
        button.setBackgroundResource(R.drawable.bg_corner_primary);
        int color = mContext.getResources().getColor(R.color.white);
        button.setTextColor(color);
        button.setTextSize(18);
        button.setTag(Item.BANK);
        button.setOnClickListener(this);
        return button;
    }

    private List<HomePayModeItem> getPayModeItemList() {
        List<HomePayModeItem> list = new ArrayList<>();
        // 银行卡
        HomePayModeItem item = new HomePayModeItem(mContext);
        item.setTag(Item.BANK);
        item.setData(R.drawable.platform_bank_card_big, R.string.platform_bank_card);
        list.add(item);

        // 商户扫客户
        item = new HomePayModeItem(mContext);
        item.setTag(Item.ALL_PAY_SCAN);
        item.setData(R.drawable.platform_scan_one_scan, R.string.platform_scan_one_scan);
        list.add(item);

        // 客户扫商户
        item = new HomePayModeItem(mContext);
        item.setTag(Item.ALL_PAY_CODE);
        item.setData(R.drawable.platform_code, R.string.platform_code);
        list.add(item);

        return list;
    }

    @Override
    public void onClick(View v) {
        boolean isCheck = mParamsCheckExt.check(true, true, true, true);
        if (isCheck) return;

        String amount = mAmountInputView.getAmountText().toString();
        isCheck = TextUtils.equals("0.00", amount);
        if (isCheck) {
            MToast.showToast(R.string.money_input_hint);
            return;
        }

        Item tag = (Item) v.getTag();
        switch (tag) {
            case BANK:
                ConsumeSwingCardActivity.startAction(mContext, amount);
                break;
            case ALL_PAY_SCAN:
                IPay88PaySelectActivity.startAction(mContext, amount, true);
                break;
            case ALL_PAY_CODE:
                IPay88PaySelectActivity.startAction(mContext, amount, false);
                break;
            default:
                break;
        }
    }


}
